﻿using Core.Manager;
using Core.UI;
using Core.UI.UISub;
using Core.UI.UISub.ModalWindow;
using Cysharp.Threading.Tasks;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.Example
{
    public class MenuSettingWindow : UIWindowBase
    {
        public enum SettingType
        {
            InMainMenu,
            InGaming
        }
        
        public Button changeSettingBtn;
        [Title("In MainMenu")]
        public GameObject inMenuRoot;
        public Button returnBtn;
        public Button toCharPanelBtn;

        [Title("In Gaming")] 
        public GameObject inGamingRoot;
        public Button returnToMainMenuBtn;
        public Button resumeBtn;

        private SettingType _curSettingType;
        private GameObject _curSettingContent;

        protected override void Awake()
        {
            base.Awake();
            changeSettingBtn.onClick.AddListener(OnClickChangeSetting);
            returnBtn.onClick.AddListener(OnClickReturnMenu);
            toCharPanelBtn.onClick.AddListener(OnClickToCharPanel);
            
            returnToMainMenuBtn.onClick.AddListener(OnClickReturnToMenu);
            resumeBtn.onClick.AddListener(OnClickResume);
            
            SetSettingType(SettingType.InMainMenu);
        }

        private void Start()
        {
        }

        public override void OnShow()
        {
            base.OnShow();
        }

        #region Btn Listen Methods

        private void OnClickToCharPanel()
        {
            UIManager.Ins.ShowUI(UIPanelName.CharacterPanel).Forget();
        }

        private void OnClickResume()
        {
            this.HideSelf();
        }

        private async void OnClickReturnToMenu()
        {
            this.ForceHideSelf();
            await GameSceneManager.Ins.LoadScene("ui_1", 0.0f);
            UIManager.Ins.SetUI(UIPanelName.MenuPanel, true).Forget();
        }

        private void OnClickReturnMenu()
        {
            this.HideSelf();
        }

        private async void OnClickChangeSetting()
        {
            var ui = await UIManager.Ins.ShowUI(UITipsPopupWindow.TipsWindow, ui =>
            {
                UITipsPopupWindow tipsWindow = (UITipsPopupWindow)ui;
                
                if (_curSettingType == SettingType.InMainMenu) tipsWindow.SetTips("Are you sure you want to change the Settings ?");
                else tipsWindow.SetTips("Are you sure you want to change the in-game Settings ?", alternation: "Reset In-Game Setting");
                
            });
            ui.OptionalCallback(choice =>
            {
                if (_curSettingType == SettingType.InMainMenu)
                {
                    if (choice == 1)
                        Debug.Log("成功修改设置！");
                    else
                        Debug.Log("未修改设置！");
                }
                else
                {
                    if (choice == 1)
                        Debug.Log("成功修改游戏内设置");
                    else if (choice == 2)
                        Debug.Log("取消修改游戏内设置");
                    else 
                        Debug.Log("重置游戏内设置");
                }
            });
        }

        #endregion

        public void SetSettingType(SettingType settingType)
        {
            _curSettingType = settingType;
            if (_curSettingContent != null)
                _curSettingContent.SetActive(false);
            switch (settingType)
            {
                case SettingType.InMainMenu:
                    _curSettingContent = inMenuRoot;
                    break;
                case SettingType.InGaming:
                    _curSettingContent = inGamingRoot;
                    break;
            }
            
            _curSettingContent.SetActive(true);
        }
    }
}